﻿using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;

namespace Hopscotch.Presentation.Common.Converters
{
    /// <summary>
    /// FieldEditModeToBrushConverter
    /// </summary>
    /// <example>
    /// <cntrlsCore:PriceControl.Background>
    ///    <MultiBinding Converter = "{cmnCnvrtrs:FieldEditModeToBrushConverter}" >
    ///        <Binding Path="{}"/><!--Bind to IToolViewModel-->
    ///        <Binding Source = "Field" />
    ///        <Binding Path="Field" />
    ///    </MultiBinding>/    
    /// </example>
    public class FieldEditModeToBrushConverter : MarkupExtension, IMultiValueConverter
    {
        public Brush EditDefaultBrush { get; set; }
        public Brush EditNewBrush { get; set; }
        public Brush EditUpdateBrush { get; set; }
        public Brush EditDeleteBrush { get; set; }

        public FieldEditModeToBrushConverter()
        {
            //Set the default brush colors
            EditDefaultBrush = Brushes.White;
            EditNewBrush = Brushes.AliceBlue;
            EditUpdateBrush = Brushes.BlanchedAlmond;
            EditDeleteBrush = Brushes.RosyBrown;
        }


        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            ToolEditMode editMode = ToolEditMode.None;

            if (values.Length > 2)
            {
                var toolViewModel = values[0] as IToolViewModel;
                string key = values[1] as string;

                if (toolViewModel != null && !string.IsNullOrEmpty(key))
                {
                    editMode = toolViewModel.GetFieldEditMode(key);
                }
            }

            switch (editMode)
            {
                case ToolEditMode.New:
                    return EditNewBrush;
                case ToolEditMode.Update:
                    return EditUpdateBrush;
                case ToolEditMode.Delete:
                    return EditDeleteBrush;
                case ToolEditMode.None:
                default:
                    return EditDefaultBrush;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
